package com.rainliang.project.user.basic.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.rainliang.project.database.Db;
import com.rainliang.project.user.basic.UserConstant;
import com.rainliang.project.user.basic.bean.LoginBean;
import com.rainliang.project.user.basic.bean.RegistBean;
import com.rainliang.project.user.basic.bean.UserBean;
import com.rainliang.project.user.basic.bean.UserInfo;
import com.rainliang.project.user.basic.dao.UserDao;
import com.rainliang.project.util.DateUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public Long addUser(UserBean userBean) {
		
		return null;
	}

	@Override
	public UserBean getById(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserInfo login(LoginBean loginBean) {
		String sql="select * from user where login_name=? and passwd=?";
		List<Map<String,Object>> result=Db.executeQuery(sql, new Object[]{loginBean.getName(),loginBean.getPassword()});
		return UserInfo.paraResult(result);
	}

	@Override
	public UserInfo register(RegistBean registBean) {
		String sql="insert into user(login_name,login_email,passwd,nick_name,sex,state,birthday,create_time) values (?,?,?,?,?,?,?,NOW())";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(registBean.getUserName());
		paramList.add(registBean.getEmail());
		paramList.add(registBean.getPasswd());
		paramList.add(registBean.getNickName());
		paramList.add(registBean.getSex());
		paramList.add(UserConstant.USER_STATE_ACTIVATE_NO);
		paramList.add(registBean.getBirthDay());
//		paramList.add(DateUtil.getDate());
		
		long uid=Db.executeUpdate(sql, paramList.toArray());
		
		if(uid>0){
			return getUserInfoById(uid);
		}
		return null;
	}

	public UserInfo getUserInfoById(Long id){
		String sql="select * from user where id=?";
		List<Map<String,Object>> result=Db.executeQuery(sql, new Object[]{id});
		return UserInfo.paraResult(result);
	}
}
